package com.example.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@Slf4j
@RestController
public class HelloController {
    @SentinelResource(value = "hello", blockHandler = "helloBlockHandler",
            blockHandlerClass = MyBlockHandler.class)
    @GetMapping("/hello")
    public String hello() {
        return "Hello sentinel!";
    }



    @SentinelResource(value = "order",fallback = "orderFallback",fallbackClass = MyFallbackHandler.class)
    @GetMapping("/order")
    public String order() throws InterruptedException {
        if(new Random().nextInt(2)==0){
//            throw new RuntimeException("出错异常");
            Thread.sleep(2000);
        }
        System.out.println(Thread.currentThread().getName()+"等待"+
                System.currentTimeMillis());

        log.info("Thread:{},time:{}",Thread.currentThread().getName(),System.currentTimeMillis());
        return "这是订单接口!";
    }

    @GetMapping("/payment")
    public String payment(){
        return "这是支付接口!";
    }
}
